package com.dpiinc.TechPage.api;

import android.content.Context;
import android.util.Log;

import com.dpiinc.TechPage.model.BaseEntity;
import com.dpiinc.TechPage.widget.LoadingView;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by hu489_000 on 2018-07-05.
 */

public abstract class BaseObserver<T> implements Observer<BaseEntity<T>> {

    private static final String TAG = "BaseObserver";
    private Context mContext;

    protected BaseObserver(Context context) {
        this.mContext = context.getApplicationContext();
    }

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(BaseEntity<T> value) {
        LoadingView.dismissLoadingView();
        if (value.getErrorCode() == 0) {
            T t = value.getResult();
            onHandleSuccess(t);
        } else {
            Log.i(TAG, "onNext: " + value.getErrorCode());
            onHandleError(value.getErrorCode());
        }
    }

    @Override
    public void onError(Throwable e) {
        Log.e(TAG, "error:" + e.toString());
    }

    @Override
    public void onComplete() {
        Log.d(TAG, "onComplete");
    }


    protected abstract void onHandleSuccess(T t);

    protected void onHandleError(int code) {
        Log.d(TAG, "onHandleError code = " + code);
    }

}

